import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源对象（datasource）
        DataSource dataSource = new MysqlDataSource();//向上转型
        // 向下转型
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("dd.159178280");

        //2，和数据库服务器进行通信，需要建立网络连接connection
        Connection connection = dataSource.getConnection();

        //3.构造SQL语句
        String sql = "delete from student2 where id is null";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //4.执行SQL语句
        //增、删、改，建表查表都是update
        //删除是 executeQuery();

        //返回的值表示操作影响到n行
        int n = preparedStatement.executeUpdate();
        System.out.println("n = " + n);

        //5.关闭连接
        preparedStatement.close();
        connection.close();
    }
}
